<?php
namespace Home\Controller;
use Think\Controller;

class UserController extends Controller{
	private $User;
	
	function __construct(){
		parent::__construct();
		header("Content-type:text/html; charset=utf-8");
		$this->User = D('Users');
	}
	
	function index(){
		$httpData	= $_REQUEST;
		$this->$httpData['func']($httpData);
	}
	/*用户登录*/
	function toLogin(){
		if($_POST){
			$username 	= $_POST['username'];
			$password 	= $_POST['password'];
			$remeberMe	= $_POST['remeberMe'];
			
			$data = array(
					'username'	=> $username,
					'password'	=> md5($password)
			);
		
			$status = $this->User->toLogin($data,$remeberMe);
		
			if($status != 10){
				$this->error($status);
			}
		
			$userData = session(C('WEB_PRE')."user");
				
			$this->redirect("/Home/Index/index");
			
		}else{
			$this->display('login');
		}
	}
	
	/*新用户注册*/
	function toSignUp(){
		if($_POST){
			$username = $_POST['username'];
			$password = $_POST['password'];
			
			$data = array(
					'username'	=> $username,
					'password'	=> md5($password),
					'nickname'	=> substr($username, 0, strpos($username, '@')),
					'createdate'=> strtotime(date("Y-m-d H:i:s")),
			);
			
			$status = $this->User->toSignUp($data);
			
			if($status != 10){
				$this->error($status);
			}
			
			$userData = session(C('WEB_PRE')."user");
			
			$this->redirect('/Home/Index/index');
			
		}else{
			$this->display('signup');	
		}
	}
	
	/*用户更改密码*/
	function changePwd($httpData = null){
		if($httpData != null){
			$data = array(
				'old_pwd'	=> $httpData['old_pwd'],
				'new_pwd'	=> $httpData['new_pwd']
			);
		
			$status = $this->User->changePwd($data);
		
			if($status != 10){
				$this->error($status);	return false;
			}
		
			$userData = session(C('WEB_PRE')."user");
			echo "密码更改成功!";
		}else{
			$this->display("changePwd");
		}
	}
	
	/*更改个人信息*/
	function reInfo(){
		if($_POST){
			$data = $_POST;
		
			$status = $this->User->reInfo($data);
		
			$userDate = session(C('WEB_PRE')."user");
		
			$this->redirect("Index/index");
		}else{
			$this->display("setuser");
		}
	}
	
	/*退出登录*/
	function toLogout(){
		cookie(C('WEB_PRE').'username', NULL);
		cookie(C('WEB_PRE').'password', NULL);
		$this->redirect("/Home/User/toLogin");
	}
	
}